{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# COCO data preprocessing\n",
    "\n",
    "This code will download the caption anotations for coco and preprocess them into an hdf5 file and a json file. \n",
    "\n",
    "These will then be read by the COCO data loader in Lua and trained on."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import json\n",
    "test = json.load(open('annotations/image_info_test2014.json', 'r'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[u'info', u'images', u'licenses', u'categories']\n",
      "{u'description': u'This is stable 1.0 version of the 2014 MS COCO dataset.', u'url': u'http://mscoco.org', u'version': u'1.0', u'year': 2014, u'contributor': u'Microsoft COCO group', u'date_created': u'2015-11-11 02:11:36.777541'}\n",
      "40775\n",
      "{u'license': 2, u'file_name': u'COCO_test2014_000000523573.jpg', u'coco_url': u'http://mscoco.org/images/523573', u'height': 500, u'width': 423, u'date_captured': u'2013-11-14 12:21:59', u'id': 523573}\n"
     ]
    }
   ],
   "source": [
    "print test.keys()\n",
    "print test['info']\n",
    "print len(test['images'])\n",
    "print test['images'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "# combine all images and annotations together\n",
    "imgs = test['images']\n",
    "\n",
    "# create the json blob\n",
    "out = []\n",
    "for i,img in enumerate(imgs):\n",
    "    imgid = img['id']\n",
    "    \n",
    "    # coco specific here, they store train/val images separately\n",
    "    loc = 'test2014'\n",
    "    \n",
    "    jimg = {}\n",
    "    jimg['file_path'] = os.path.join(loc, img['file_name'])\n",
    "    jimg['id'] = imgid\n",
    "    \n",
    "    out.append(jimg)\n",
    "    \n",
    "json.dump(out, open('coco_test_raw.json', 'w'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "u'val2014/COCO_test2014_000000155724.jpg'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jimg['file_path']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'file_path': u'val2014/COCO_test2014_000000523573.jpg', 'id': 523573}\n"
     ]
    }
   ],
   "source": [
    "# lets see what they look like\n",
    "print out[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
